package com.design.twords.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.design.twords.R;
import com.design.twords.adatper.WordBookAdapter;
import com.design.twords.config.ConstantValue;
import com.design.twords.database.BookList;
import com.design.twords.entity.ItemWordBook;
import com.design.twords.utils.MyApplication;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  22:45
 */
public class BooklListActivity extends AppCompatActivity {

    private RecyclerView book_list,book_hasdown_list;
    private LinearLayout booklist_bottomview;

    private WordBookAdapter booklistAdapter,bookdownAdapter;

    private ArrayList<ItemWordBook> wordBooks = new ArrayList<>();
    private ArrayList<ItemWordBook> wordBooks_hasdown = new ArrayList<>();

    private static final int PERMISSION_REQUESTCODE = 100;

    private int[] bookIds = new int[]{ConstantValue.CET_4, ConstantValue.CET_6, ConstantValue.KAOYAN,
            ConstantValue.CET_6_ALL, ConstantValue.KAOYAN_ALL};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booklist);
        MyApplication.getInstance().addActivity(this);
        initView();
        initDate();

        JudgePermission();

        book_list.setLayoutManager(new LinearLayoutManager(this));
        booklistAdapter = new WordBookAdapter(this, wordBooks, R.layout.item_booklist);
        book_list.setAdapter(booklistAdapter);

        book_hasdown_list.setLayoutManager(new LinearLayoutManager(this));
        bookdownAdapter = new WordBookAdapter(this, wordBooks_hasdown, R.layout.item_booklist);
        book_hasdown_list.setAdapter(bookdownAdapter);

        bookdownAdapter.setListener(new WordBookAdapter.ItemClickListener() {
            @Override
            public void itemLongClickListener(int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(BooklListActivity.this);
                builder.setItems(new String[]{"删除"}, (DialogInterface.OnClickListener) (dialog, which) -> {
                    bookdownAdapter.deleteItem(position);
                    Toast.makeText(BooklListActivity.this, "删除成功", Toast.LENGTH_LONG).show();

                    onRestart();

                }).show();

               }
        });



    }

    private void initDate() {
        List<BookList> bookname = LitePal.findAll(BookList.class);
        ArrayList<Integer> type = new ArrayList<>();
        wordBooks_hasdown.clear();
        wordBooks.clear();
        if(bookname.size() >0){
            //将已下载的书籍数据保存到wordBooks_hasdown中
            for(BookList bo:bookname){
                int id = bo.getBookId();
                type.add(bo.getBookId());
                ItemWordBook item = new ItemWordBook(id, ConstantValue.bookNameById(id), ConstantValue.wordTotalNumberById(id),"有道考神",ConstantValue.bookPicById(id),true);
                wordBooks_hasdown.add(item);
            }
        }
        for (int bi:bookIds){
            if(!type.contains(bi)){
                //将没有下载的书保存到wordBooks中
                ItemWordBook item = new ItemWordBook(bi, ConstantValue.bookNameById(bi), ConstantValue.wordTotalNumberById(bi),"有道考神",ConstantValue.bookPicById(bi),false);
                wordBooks.add(item);
            }
        }
    }

    private void initView() {
        book_list = findViewById(R.id.book_list);
        book_hasdown_list = findViewById(R.id.book_hasdown_list);

    }

    private void JudgePermission(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission_group.STORAGE},
                    PERMISSION_REQUESTCODE);
            // 未获得权限
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.CAMERA)) {
                //说明用户明确拒绝了某个权限，我们应该在这里做出提示给用户，说清楚此权限的用户
                Toast.makeText(BooklListActivity.this,"没有权限",Toast.LENGTH_LONG).show();
                onStart();

            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode != PERMISSION_REQUESTCODE) {
            Toast.makeText(BooklListActivity.this,"没有存储权限",Toast.LENGTH_LONG).show();
            onBackPressed();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        initDate();
        booklistAdapter.setWordBooks(wordBooks);
        booklistAdapter.notifyDataSetChanged();
        book_list.setAdapter(booklistAdapter);

        bookdownAdapter.setWordBooks(wordBooks_hasdown);
        bookdownAdapter.notifyDataSetChanged();
        book_hasdown_list.setAdapter(bookdownAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }
}
